      PROGRAM HBREAD

      IMPLICIT NONE

      INTEGER NWPAWC,IPAW,IQUEST

      PARAMETER (NWPAWC = 400000000)
      COMMON/PAWC/IPAW(NWPAWC)
      COMMON/QUEST/IQUEST(100)
      INTEGER ISTAT,NFILE,K,LREC
      CHARACTER*1024 NtFileName
      CHARACTER*1024 output

      REAL*4 TimeStart,TimeEnd

      output = "gtntp.hbook"

      NFILE = IARGC()
      IF (NFILE .lt. 1) THEN
         WRITE(*,'(a)') "usage: hbread file1.hbook [file2.hbook [...]]"
         STOP
      ENDIF

      WRITE(*,*) "Using ", NFILE, " HBOOK files as input"

      CALL TIMEX(TimeStart)
      CALL HLIMIT(NWPAWC)

      K=1
 100  IF (K .le. NFILE) THEN
         CALL GETARG(K,NtFileName)
         K=K+1

         IF (INDEX(NtFileName, "-") .eq. 1) THEN
            IF (NtFileName .eq. "-o") THEN
               CALL GETARG(K, output)
               K=K+1
               write (*,*) "Output will be left in ", output(1:LEN_TRIM(output))
               GOTO 100
            ELSE
               write (*,*) "Option is not recognized: ", NtFileName(1:LEN_TRIM(NtFileName))
               STOP
            ENDIF
         ENDIF

c         WRITE (*,*) "Opening ", NtFileName, K, NFILE

         CALL HROPEN(70, 'FILE', NtFileName, ' ', 32768, ISTAT)

         IF (ISTAT.NE.0) THEN
            WRITE (*,*) 'Error opening file ', NtFileName(1:LEN_TRIM(NtFileName))
            WRITE (*,*) 'Aborting...'

            STOP
         ENDIF

         CALL HCDIR(' ',' ')

         CALL HRIN(1,99999999,0)
         
         CALL HB(K,NtFileName)
         
         CALL HREND('FILE')

         CLOSE(0)

         GOTO 100
      ENDIF


      CALL HRPUT(0,output,' ')

      CALL TIMEX(TimeEnd)

      PRINT *,'Time used ',TimeEnd - TimeStart,' s'

      END




